package org.java.admin.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.Map;

/**
 * @program: lb-manage
 * @description
 * @author: LiuBao
 * @create: 2020-07-30 10:06
 **/
public class ChargeTriggerUtil {

    /**
     * 创建定时任务
     * @param job
     * @param map
     * @param key
     * @param date
     */
    public static void createTask(Job job, Map<String, Object> map, String key, Date date) throws SchedulerException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
        //创建唯一标识
        JobKey jobKey = new JobKey(key);
        //数据传输
        JobDataMap jobDataMap = new JobDataMap(map);
        JobDetail jobDetail = JobBuilder.newJob(job.getClass()).withIdentity(jobKey).setJobData(jobDataMap).build();
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity(key).startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();

        scheduler.scheduleJob(jobDetail, trigger);
    }

    /**
     * 删除定时任务
     * @param key
     */
    public static void deleteTask(String key) {
        Scheduler scheduler = null;
        try {
            scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.deleteJob(new JobKey(key));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
